﻿// #define USE_OLD_SP

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;
using UDS.Entity;

namespace UDS.SubModule.UnitiveDocument.DocumentFlowPages
{
    /// <summary>
    /// BangdingRole 的摘要说明。
    /// </summary>
    public partial class BindStaffPage : Page
    {
        public int FlowID;
        public int StepID;

        protected override void OnInit(EventArgs e)
        {
            cmdAdd.Click += cmdAdd_Click;
            cmdDelete.Click += cmdDelete_Click;
            cmdAddPositon.Click += cmdAddPositon_Click;
            cmdDeletePosition.Click += cmdDeletePosition_Click;
            cmdAddTeam.Click += cmdAddTeam_Click;
            cmdDeleteTeam.Click += cmdDeleteTeam_Click;
            cmdAddTeamLeader.Click += cmdAddTeamLeader_Click;
            cmdDeleteTeamLeader.Click += cmdDeleteTeamLeader_Click;
            cmdAddStaff.Click += cmdAddStaff_Click;
            cmdDeleteStaff.Click += cmdDeleteStaff_Click;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            FlowID = QSUtil.GetPositiveInt32("FlowID");
            StepID = QSUtil.GetPositiveInt32("StepID");

            returnButton.RedirectUrl = "ManageFlow.aspx?FlowID=" + FlowID;
            if (!Page.IsPostBack)
            {
                Bangding();
            }

            base.OnLoad(e);
        }

        private SqlDataReader RunDataReader(string spName)
        {
            var mySQL = new Database();

            SqlParameter[] parameters = {
                Database.MakeInParam("@FlowID", SqlDbType.Int, 4, FlowID),
                Database.MakeInParam("@StepID", SqlDbType.Int, 4, StepID)
            };

            return mySQL.RunDataReader(spName, parameters);
        }

        private void InitRoles()
        {
            using (var dr = RunDataReader("dbo.sproc_Flow_GetBangdingRole_V2"))
            {
                lstCurRole.Items.Clear();
                lstAllRole.Items.Clear();
                while (dr.Read())
                {
                    ListItem listItem = new ListItem(dr["role_name"].ToString(), dr["role_id"].ToString());
                    if (Convert.ToBoolean(dr["Bound"]))
                    {
                        lstCurRole.Items.Add(listItem);
                    }
                    else
                    {
                        lstAllRole.Items.Add(listItem);
                    }
                }
            }
        }

        private void InitPositions()
        {
            using (var dr = RunDataReader("dbo.sproc_Flow_GetBangdingPosition_V2"))
            {
                lstCurPosition.Items.Clear();
                lstAllPosition.Items.Clear();

                while (dr.Read())
                {
                    ListItem listItem = new ListItem(dr["Position_name"].ToString(), dr["Position_id"].ToString());
                    if (Convert.ToBoolean(dr["Bound"]))
                    {
                        lstCurPosition.Items.Add(listItem);
                    }
                    else
                    {
                        lstAllPosition.Items.Add(listItem);
                    }
                }
            }
        }

        private void InitTeams()
        {
            using (var dr = RunDataReader("dbo.sproc_Flow_GetBangdingTeam_V2"))
            {
                lstCurTeam.Items.Clear();
                lstAllTeam.Items.Clear();
                while (dr.Read())
                {
                    ListItem listItem = new ListItem(dr["Team_name"].ToString(), dr["Team_id"].ToString());
                    if (Convert.ToBoolean(dr["Bound"]))
                    {
                        lstCurTeam.Items.Add(listItem);
                    }
                    else
                    {
                        lstAllTeam.Items.Add(listItem);
                    }
                }
            }
        }

        private void InitTeamLeaders()
        {
            using (var dr = RunDataReader("dbo.sproc_Flow_GetBangdingTeamLeader_V2"))
            {
                lstCurTeamLeader.Items.Clear();
                lstAllTeamLeader.Items.Clear();
                while (dr.Read())
                {
                    ListItem listItem = new ListItem(dr["Team_name"].ToString(), dr["Team_id"].ToString());
                    if (Convert.ToBoolean(dr["Bound"]))
                    {
                        lstCurTeamLeader.Items.Add(listItem);
                    }
                    else
                    {
                        lstAllTeamLeader.Items.Add(listItem);
                    }
                }
            }
        }

        private void InitStaffs()
        {
            using (var dr = RunDataReader("dbo.sproc_Flow_GetBangdingStaff_V2"))
            {
                lstCurStaff.Items.Clear();
                lstAllStaff.Items.Clear();
                while (dr.Read())
                {
                    ListItem listItem = new ListItem(dr["Staff_name"].ToString(), dr["Staff_id"].ToString());
                    if (Convert.ToBoolean(dr["Bound"]))
                    {
                        lstCurStaff.Items.Add(listItem);
                    }
                    else
                    {
                        lstAllStaff.Items.Add(listItem);
                    }
                }
            }
        }

        private void InitAllMember()
        {
            using (var dr = RunDataReader("dbo.sproc_Flow_GetAllBindMember_V2"))
            {
                lstAllMember.Items.Clear();
                while (dr.Read())
                {
                    lstAllMember.Items.Add(new ListItem(dr["RealName"].ToString(), dr["Staff_id"].ToString()));
                }
            }
        }

        private void InitHeadLine()
        {
            labTitle.Text = Flow.GetFlowTitle(FlowID) + " -&gt; " + FlowStep.GetStepTitle(FlowID, StepID);
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            if (lstAllRole.SelectedIndex >= 0)
            {
                var mySQL = new Database();
                foreach (ListItem li in lstAllRole.Items)
                {
                    if (li.Selected)
                    {
                        SqlParameter[] parameters = {
                            Database.MakeInParam("@FlowID", SqlDbType.Int, 4, FlowID),
                            Database.MakeInParam("@StepID", SqlDbType.Int, 4, StepID),
                            Database.MakeInParam("@RoleID", SqlDbType.Int, 4, Int32.Parse(li.Value))
                        };

                        mySQL.RunProc("dbo.sproc_Flow_AddBangdingRole_V2", parameters);
                    }
                }
                mySQL.Close();
                //Response.Write("<script language='javascript'>alert('绑定成功！');</script>");
                Bangding();
            }
        }

        private void cmdDelete_Click(object sender, EventArgs e)
        {
            if (lstCurRole.SelectedIndex >= 0)
            {
                var mySQL = new Database();
                foreach (ListItem li in lstCurRole.Items)
                {
                    if (li.Selected)
                    {
                        SqlParameter[] parameters = {
                            Database.MakeInParam("@FlowID", SqlDbType.Int, 4, FlowID),
                            Database.MakeInParam("@StepID", SqlDbType.Int, 4, StepID),
                            //mySQL.MakeInParam("@RoleID",SqlDbType.Int ,4,Int32.Parse(lstCurRole.Items[lstCurRole.SelectedIndex].Value)  )
                            Database.MakeInParam("@RoleID", SqlDbType.Int, 4, Int32.Parse(li.Value))
                        };

                        mySQL.RunProc("dbo.sproc_Flow_DeleteBangdingRole_V2", parameters);
                    }
                }
                mySQL.Close();
                //Response.Write("<script language='javascript'>alert('取消绑定成功！');</script>");		
                Bangding();
            }
        }

        private void cmdAddPositon_Click(object sender, EventArgs e)
        {
            if (lstAllPosition.SelectedIndex >= 0)
            {
                var mySQL = new Database();
                foreach (ListItem li in lstAllPosition.Items)
                {
                    if (li.Selected)
                    {
                        SqlParameter[] parameters = {
                            Database.MakeInParam("@FlowID", SqlDbType.Int, 4, FlowID),
                            Database.MakeInParam("@StepID", SqlDbType.Int, 4, StepID),
                            Database.MakeInParam("@PositionID", SqlDbType.Int, 4, Int32.Parse(li.Value))
                        };

                        mySQL.RunProc("dbo.sproc_Flow_AddBangdingPosition_V2", parameters);
                    }
                }
                mySQL.Close();
                Bangding();
            }
        }

        private void cmdDeletePosition_Click(object sender, EventArgs e)
        {
            if (lstCurPosition.SelectedIndex >= 0)
            {
                var mySQL = new Database();
                foreach (ListItem li in lstCurPosition.Items)
                {
                    if (li.Selected)
                    {
                        SqlParameter[] parameters = {
                            Database.MakeInParam("@FlowID", SqlDbType.Int, 4, FlowID),
                            Database.MakeInParam("@StepID", SqlDbType.Int, 4, StepID),
                            Database.MakeInParam("@PositionID", SqlDbType.Int, 4, Int32.Parse(li.Value))
                        };

                        mySQL.RunProc("dbo.sproc_Flow_DeleteBangdingPosition_V2", parameters);
                    }
                }
                mySQL.Close();
                //Response.Write("<script language='javascript'>alert('取消绑定成功！');</script>");		
                Bangding();
            }
        }

        private void cmdDeleteTeam_Click(object sender, EventArgs e)
        {
            if (lstCurTeam.SelectedIndex >= 0)
            {
                var mySQL = new Database();
                foreach (ListItem li in lstCurTeam.Items)
                {
                    if (li.Selected)
                    {
                        SqlParameter[] parameters = {
                            Database.MakeInParam("@FlowID", SqlDbType.Int, 4, FlowID),
                            Database.MakeInParam("@StepID", SqlDbType.Int, 4, StepID),
                            Database.MakeInParam("@TeamID", SqlDbType.Int, 4, Int32.Parse(li.Value))
                        };

                        mySQL.RunProc("dbo.sproc_Flow_DeleteBangdingTeam_V2", parameters);
                    }
                }
                mySQL.Close();
                //Response.Write("<script language='javascript'>alert('取消绑定成功！');</script>");		
                Bangding();
            }
        }

        private void cmdDeleteStaff_Click(object sender, EventArgs e)
        {
            if (lstCurStaff.SelectedIndex >= 0)
            {
                var mySQL = new Database();
                foreach (ListItem li in lstCurStaff.Items)
                {
                    if (li.Selected)
                    {
                        SqlParameter[] parameters = {
                            Database.MakeInParam("@FlowID", SqlDbType.Int, 4, FlowID),
                            Database.MakeInParam("@StepID", SqlDbType.Int, 4, StepID),
                            Database.MakeInParam("@StaffID", SqlDbType.Int, 4, Int32.Parse(li.Value))
                        };

                        mySQL.RunProc("dbo.sproc_Flow_DeleteBangdingStaff_V2", parameters);
                    }
                }
                mySQL.Close();
                //Response.Write("<script language='javascript'>alert('取消绑定成功！');</script>");		
                Bangding();
            }
        }

        private void cmdAddTeam_Click(object sender, EventArgs e)
        {
            if (lstAllTeam.SelectedIndex >= 0)
            {
                var mySQL = new Database();
                foreach (ListItem li in lstAllTeam.Items)
                {
                    if (li.Selected)
                    {
                        SqlParameter[] parameters = {
                            Database.MakeInParam("@FlowID", SqlDbType.Int, 4, FlowID),
                            Database.MakeInParam("@StepID", SqlDbType.Int, 4, StepID),
                            Database.MakeInParam("@TeamID", SqlDbType.Int, 4, Int32.Parse(li.Value))
                        };

                        mySQL.RunProc("dbo.sproc_Flow_AddBangdingTeam_V2", parameters);
                    }
                }
                mySQL.Close();
                Bangding();
            }
        }

        private void cmdAddStaff_Click(object sender, EventArgs e)
        {
            if (lstAllStaff.SelectedIndex >= 0)
            {
                var mySQL = new Database();
                foreach (ListItem li in lstAllStaff.Items)
                {
                    if (li.Selected)
                    {
                        SqlParameter[] parameters = {
                            Database.MakeInParam("@FlowID", SqlDbType.Int, 4, FlowID),
                            Database.MakeInParam("@StepID", SqlDbType.Int, 4, StepID),
                            Database.MakeInParam("@StaffID", SqlDbType.Int, 4, Int32.Parse(li.Value))
                        };

                        mySQL.RunProc("dbo.sproc_Flow_AddBangdingStaff_V2", parameters);
                    }
                }
                mySQL.Close();
                Bangding();
            }
        }

        private void cmdAddTeamLeader_Click(object sender, EventArgs e)
        {
            if (lstAllTeamLeader.SelectedIndex >= 0)
            {
                var mySQL = new Database();
                foreach (ListItem li in lstAllTeamLeader.Items)
                {
                    if (li.Selected)
                    {
                        SqlParameter[] parameters = {
                            Database.MakeInParam("@FlowID", SqlDbType.Int, 4, FlowID),
                            Database.MakeInParam("@StepID", SqlDbType.Int, 4, StepID),
                            Database.MakeInParam("@TeamID", SqlDbType.Int, 4, Int32.Parse(li.Value))
                        };

                        mySQL.RunProc("dbo.sproc_Flow_AddBangdingTeamLeader_V2", parameters);
                    }
                }
                mySQL.Close();
                Bangding();
            }
        }

        private void cmdDeleteTeamLeader_Click(object sender, EventArgs e)
        {
            if (lstCurTeamLeader.SelectedIndex >= 0)
            {
                var mySQL = new Database();
                foreach (ListItem li in lstCurTeamLeader.Items)
                {
                    if (li.Selected)
                    {
                        SqlParameter[] parameters = {
                            Database.MakeInParam("@FlowID", SqlDbType.Int, 4, FlowID),
                            Database.MakeInParam("@StepID", SqlDbType.Int, 4, StepID),
                            Database.MakeInParam("@TeamID", SqlDbType.Int, 4, Int32.Parse(li.Value))
                        };

                        mySQL.RunProc("dbo.sproc_Flow_DeleteBangdingTeamLeader_V2", parameters);
                    }
                }
                mySQL.Close();
                //Response.Write("<script language='javascript'>alert('取消绑定成功！');</script>");		
                Bangding();
            }
        }

        #region 绑定数据库

        private void Bangding()
        {
            InitHeadLine();

            InitRoles();
            InitPositions();
            InitTeams();
            InitTeamLeaders();
            InitStaffs();

            InitAllMember();
        }

        #endregion
    }
}